package com.xybbs.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.FetchType;

import com.xybbs.entity.Tips;

public interface TipsMapper {

	@Insert("insert into tip(userId,forumId,tipTitle,tipContent,tipPublishTime,"
			+ "tipModifyTime)values(#{userId},#{forumId},#{tipTitle},#{tipContent},#{tipPublishTime}, #{tipModifyTime})")
	void insert(Tips tip);

	@Results({
		 	@Result(column = "userId", property = "userId"),
			@Result(column = "userId", property = "user", one = @One(select = "com.xybbs.mapper.UsersMapper.selectUserById", fetchType = FetchType.LAZY)) })
	@Select("select * from tip")
	List<Tips> selectAllWithUser();
	
	@Select("select * from tip")
	List<Tips> selectAllWithUser1();


	@Select("select * from tip where userId = #{userId}")
	List<Tips> selectTipsByUserId(String userId);

	@Select("select * from tip where tipId = #{tipId}")
	@Results({ 
			@Result(column = "userId", property = "userId"),
			@Result(column = "userId", property = "user", one = @One(select = "com.xybbs.mapper.UsersMapper.selectUserById", fetchType = FetchType.LAZY)) })
	Tips selectTipByIdWithUser(String tipId);

	@Update("update tip set tipClick = tipClick + 1 where tipId = #{tipId}")
	void updateTipClickById(String tipId);

	@Update("update tip set tipReplies = tipReplies+1 where tipId = #{tipId}")
	void updateTipRepliesById(String tipId);

	@Update("update tip set tipReplies = tipReplies-1 where tipId = #{tipId}")
	void updateTipRepliesWhenReove(String tipId);
	
	@Update("update tip set tipIsDeleted = #{code} where tipId = #{tipId}")
	void updateTipStatus(@Param("tipId")String tipId,@Param("code")String code);
	
	@Update("update tip set tipIsKnot = #{code} where tipId = #{tipId}")
	void updateTipKnot(@Param("tipId")String tipId,@Param("code")String code);

	@Select("select * from tip where tipTitle like '%${keyword}%'")
	List<Tips> selectTipByKeyword(String keyword);
}